/*
 * File Name     : @(#)CriteriaUtil.java
 * Creation Date : Jun 7, 2009
 * Author        : Anirudh Vyas
 * 
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package hcis.common.utility.persistence.hibernate;

import hcis.common.utility.IUtility;
import hcis.model.projection.IProjectionAggregationModel.AggregateType;
import hcis.model.projection.path.alias.generator.strategy.def.impl.DefaultAliasGeneratorStrategy;
import hcis.model.projection.path.def.impl.PropertyPath;
import hcis.model.projection.path.def.impl.SortPropertyPath;

import java.util.List;

import org.apache.commons.lang.Validate;
import org.hibernate.Criteria;
import org.hibernate.criterion.Order;

/**
 * Criteria utility.
 * 
 * @author rudra
 */
public final class CriteriaUtil implements IUtility {

	/**
	 * Default no argument constructor.
	 */
	private CriteriaUtil() {
		super();
	}

	/**
	 * Sets the aggregate on a projection path provided.
	 * 
	 * @param pCriteria
	 *            criteria to aggregate on.
	 * @param pProjectionPath
	 *            projection path to be used for aggregation operation.
	 * @param aggregateType
	 *            aggregate type on a projection path.
	 */
	public static final void aggregateOn(final Criteria pCriteria, final PropertyPath pProjectionPath, final AggregateType aggregateType) {
		switch (aggregateType) {
			case AVG:
				break;
			case COUNT:
				break;
			case SUM:
				break;
			case MAX:
				break;
			case MIN:
				break;
			default:
				break;
		}
	}

	/**
	 * Adds hydration paths.
	 * 
	 * @param pCriteria
	 *            criteria to be processed for adding hydration paths.
	 * @param pPropertyPaths
	 *            property paths for hydration.
	 */
	public static final void addHydrationPaths(final Criteria pCriteria, final List<PropertyPath> pPropertyPaths) {
		Validate.notEmpty(pPropertyPaths);
		Validate.notNull(pCriteria);
		for (final PropertyPath path : pPropertyPaths) {
			pCriteria.createAlias(path.getQualifiedName(), new DefaultAliasGeneratorStrategy().generate(path));
		}
	}

	/**
	 * Adds Order bys using sort projection/field paths.
	 * 
	 * @param pCriteria
	 *            criteria to be processed for adding sortable projection/field paths.
	 * @param pPropertyPaths
	 *            property paths for order bys.
	 */
	public static final void addOrderBys(final Criteria pCriteria, final List<SortPropertyPath> pPropertyPaths) {
		Validate.notEmpty(pPropertyPaths);
		Validate.notNull(pCriteria);
		String pathQN = null;
		for (final SortPropertyPath path : pPropertyPaths) {
			pathQN = path.getQualifiedName();
			switch (path.getSort()) {
				case ASC:
					pCriteria.addOrder(Order.asc(pathQN));
					break;
				case DSC:
					pCriteria.addOrder(Order.desc(pathQN));
					break;
				default:
					throw new UnsupportedOperationException("Not a supported operation!");
			}
		}
	}
}
